package com.frank.demo.thread;

import com.frank.demo.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by zhy on 2017/7/14.
 */
public class StopThread {

    public static void main(String[] args) throws InterruptedException {

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        Map<Integer,TestThread> map = new HashMap<Integer, TestThread>();
        TestThread testThread1 = new TestThread();
        TestThread testThread2 = new TestThread();
        map.put(1,testThread1);
        map.put(2,testThread2);

        cachedThreadPool.execute(testThread1);
        cachedThreadPool.execute(testThread2);

//        Thread.sleep(3000L);
//        System.out.println("stop 1");
//        TestThread b = map.get(1) ;
//        b.stop = true;
//        System.out.println("set 1 = true");
    }
}

class TestThread implements Runnable{
    volatile boolean stop = false;

    public void run() {
        System.out.println(Thread.currentThread().getName() + " start");
        while (!stop){
//            System.out.println(Thread.currentThread().getName() + "                 " +  stop);

            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(Thread.currentThread().getName() + " finish");

    }
}
